package com.SocketDemo.TCPTest01.TCPTest;

/*
 * 客户端：发送数据，接收服务器反馈
 * */

import java.io.*;
import java.net.Socket;

public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建Socket对象
        Socket s = new Socket("10.10.116.157", 10000);

//        //输出流，写数据
//        OutputStream os = s.getOutputStream();
//        os.write("hello，tcp，接口撒旦教熬枯受淡".getBytes(StandardCharsets.UTF_8));

        //数据来自键盘输入，知道输入数据是886.发送数据结束
        BufferedReader br = new BufferedReader(new FileReader("/Users/caoting/IdeaProjects/untitled/ClientDemo.java"));
        //封装输出流对象
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String line;
        while ((line = br.readLine()) != null) {
//            if (line.equals("886")) {
//                break;
//            }
            //获取输出流对象，发送数据
//            OutputStream os = s.getOutputStream();
//            os.write(line.getBytes(StandardCharsets.UTF_8));
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        s.shutdownOutput();
//        //自定义结束标记
//        bw.write("886");
//        bw.newLine();
//        bw.flush();

        //接受服务器反馈
//        InputStream is = s.getInputStream();
//        byte[] bys = new byte[1024];
//        int len = is.read(bys);
//        String data = new String(bys, 0, len);
//        System.out.println(data);
        BufferedReader brclient = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String data = brclient.readLine();//等待读取数据
        System.out.println(data);

        //释放资源
        br.close();
        s.close();


    }
}
